<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0px;
      padding: 0px;
    }

    .sticky-container {
      background: #ccc;
      padding: 20px;
      margin: 20px;
    }

    .sticky-header {
      background-color: #999;
      padding: 20px;
      position: sticky;
      /* 卷动到header距离容器为0的时候就会触发机黏着 */
      top: 0;
    }

    /* 整个container滚动完，没有任何一个元素出现在页面上的时候黏着失效 */
    .long-content {
      height: 300vh;
      background: #fff;
    }
  </style>
</head>

<body>
  <div class="sticky-container">
    sticky container
    <div class="sticky-header"> sticky header</div>
    <div class="long-content"></div>
  </div>
</body>

</html>